﻿using Model.BusinessModel.InPut;
using Model.BusinessModel.OutPut;
using Model.DomainModel;
using Model.Flow;
using Model.OverallAtuhModel;
using Utility;
using Utility.Enum;

namespace CoreDomain
{
    /// <summary>
    /// 流程消息处理核心
    /// </summary>
    public class FlowMessageCore
    {

        /// <summary>
        /// 消息处理
        /// </summary>
        /// <param name="expireActionEnum">处理动作</param>
        /// <param name="flowMessage">流程消息</param>
        /// <param name="expireConfig">过期配置</param>
        public static FlowApplyExamineInput MessageHandled(ExpireActionEnum expireActionEnum, FlowMessage flowMessage, FlowNodeExpireDataOutPut expireConfig)
        {
            FlowApplyExamineInput input = new FlowApplyExamineInput();
            //无动作不做任何处理，会一直等待审核人审批
            if (expireActionEnum == ExpireActionEnum.NoAction)
                return new FlowApplyExamineInput();

            //自动批准。
            if (expireActionEnum == ExpireActionEnum.Approve)
            {
                input.ExamineState = (int)FlowNodeStateEnum.AuditPass;
                input.ExamineNodeId = flowMessage.NodeId;
                input.ExamineRemark = "超时自动通过";
                input.FlowApplyId = flowMessage.FlowApplyId;
            }
            //自动拒绝。
            if (expireActionEnum == ExpireActionEnum.AuditRefuse)
            {
                input.ExamineState = (int)FlowNodeStateEnum.AuditRefuse;
                input.ExamineNodeId = flowMessage.NodeId;
                input.ExamineRemark = "超时自动拒绝";
                input.FlowApplyId = flowMessage.FlowApplyId;
            }
            //自动驳回
            if (expireActionEnum == ExpireActionEnum.RejectNode)
            {
                var flowData = JsonHelper.JsonStrToEntity<FlowModel>(expireConfig.FlowContent);
                var previousNodelModel = FlowCore.GetPreviousFlowNodeMsg(flowData, expireConfig.CurrentNodeId);
                //当前节点存在上一个节点
                if (previousNodelModel != null)
                {
                    input.ExamineState = (int)FlowNodeStateEnum.AuditRefuse;
                    input.ExamineNodeId = flowMessage.NodeId;
                    input.ExamineRemark = "超时自动驳回到上一个节点";
                    input.FlowApplyId = flowMessage.FlowApplyId;
                    input.RejectNodeId = previousNodelModel.id; //当前节点上一个节点
                }
            }

            return input;
        }

        /// <summary>
        /// 设置流程消息--即将超时或已超时数据
        /// </summary>
        /// <param name="expireConfig">过期配置</param>
        /// <param name="expireDateModel">过期数据模型</param>
        /// <param name="messageState">消息状态</param>
        /// <returns></returns>
        public static List<FlowMessage> SetFlowMessage(FlowNodeExpireDataOutPut expireConfig, ExpireDateModel expireDateModel, MessageStateEnum messageState)
        {
            List<FlowMessage> flowMessages = new List<FlowMessage>();
            var expireStr = EnumHelper.GetDescription(messageState);
            var messageContent = messageState == MessageStateEnum.Expired ? $"流程【{expireConfig.ApplyTitle}】{expireStr}" : $"流程【{expireConfig.ApplyTitle}】{expireStr},过期后流程将【{EnumHelper.GetDescription(expireDateModel.ExpireAction)}】,{expireDateModel.RemindContent}";
            foreach (var item in expireConfig?.CurrentNodeReviewerId.Split(','))
            {
                FlowMessage flowMessage = new FlowMessage();
                flowMessage.MessageId = Guid.NewGuid().ToString();
                flowMessage.NodeName = expireConfig.CurrentNodeName ?? string.Empty;
                flowMessage.NodeId = expireConfig.CurrentNodeId ?? string.Empty;
                flowMessage.MessageRecipient = item;
                flowMessage.MessageTitle = $"流程【{expireConfig.ApplyTitle}】{expireStr}";
                flowMessage.MessageContent = messageContent;
                flowMessage.FlowApplyId = expireConfig.FlowApplyId;
                flowMessage.MessageType = (int)MessageTypeEnum.ExpireRemind;
                flowMessage.MessageState = (int)messageState;
                flowMessage.IsBrowse = false;
                flowMessage.BrowseTime = null;
                flowMessage.IsHandled = (expireDateModel.ExpireAction != ExpireActionEnum.NoAction && messageState == MessageStateEnum.Expired) ? true : false; //如果是驳回，那么必须存在上一个节点，才会标记为已处理（未实现）
                flowMessage.HandledTime = (expireDateModel.ExpireAction != ExpireActionEnum.NoAction && messageState == MessageStateEnum.Expired) ? DateTime.Now : null;
                flowMessage.HandledType = (expireDateModel.ExpireAction != ExpireActionEnum.NoAction && messageState == MessageStateEnum.Expired) ? (int)HandledTypeEnum.SystemApproval : null;
                flowMessage.HandledState = SetHandledState(expireDateModel.ExpireAction);
                flowMessage.MessageSender = "6A75EC49-2093-4B89-950F-65E6E72746BA";
                flowMessage.CreateTime = DateTime.Now;
                flowMessage.CreateUserKey = "6A75EC49-2093-4B89-950F-65E6E72746BA";
                flowMessages.Add(flowMessage);
            }
            return flowMessages;
        }

        /// <summary>
        /// 设置审批状态
        /// </summary>
        /// <param name="expireAction">超时动作</param>
        /// <returns></returns>
        private static int SetHandledState(ExpireActionEnum expireAction)
        {
            if (expireAction == ExpireActionEnum.AuditRefuse)
                return (int)FlowNodeStateEnum.AuditRefuse;
            if (expireAction == ExpireActionEnum.Approve)
                return (int)FlowNodeStateEnum.AuditPass;
            if (expireAction == ExpireActionEnum.RejectNode)
                return (int)FlowNodeStateEnum.AuditDismissal;
            if (expireAction == ExpireActionEnum.NoAction)
                return (int)FlowNodeStateEnum.InExamine;
            return (int)FlowNodeStateEnum.InExamine;
        }

        /// <summary>
        /// 获取登录信息
        /// </summary>
        /// <param name="currentNodeReviewerId">当前审核人员id</param>
        /// <param name="currentNodeReviewerName">当前审核人员名称</param>
        /// <returns></returns>
        public static LoginOutPut GetLoginOutPut(string currentNodeReviewerId, string currentNodeReviewerName)
        {
            LoginOutPut loginOutPut = new()
            {
                CorporationKey = "6A75EC49-2093-4B89-950F-65E6E72746DA",
                SystemKey = "6E746ED0-12E9-4002-9887-D84A19142305",
                ExpiresDate = "",
                Password = "",
                Token = "",
                UserId = currentNodeReviewerId,
                UserName = currentNodeReviewerName,
                RoleIds = "",
                DepartmentId = ""
            };

            return loginOutPut;
        }
    }
}
